﻿Imports WINX32Lib
Imports System
Imports System.IO

Module Functions

    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
     (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)

    Function Split_Parameters(ByVal Args_String As String, ByRef Parameters As String(), ByRef Parameter_Values As String()) As Integer
        Dim arrArgs() As String
        Dim i As Integer = Args_String.LastIndexOf(")"c, Args_String.Length - 1, Args_String.Length)
        If i.Equals(-1) Then
            arrArgs = Args_String.Substring(Args_String.IndexOf("#"c, 0) + 1).Split("#"c)
        Else
            Dim Bra_Positions, Ket_Positions As New Stack(Of Integer)
            Bra_Positions.Push(Args_String.Length)
            While Not i.Equals(-1)
                Ket_Positions.Push(i)
                i = Args_String.LastIndexOf(")", i - 1, i)
            End While
            i = Args_String.LastIndexOf("("c, Args_String.Length - 1, Args_String.Length)
            While Not i.Equals(-1)
                Bra_Positions.Push(i)
                i = Args_String.LastIndexOf("(", i - 1, i)
            End While
            If Not Bra_Positions.Count - 1 = Ket_Positions.Count Then
                MsgBox("There are unpaired brackets in the porgamme arguments")
                Return -1
            End If
            Dim Current_Bra_Position As Integer = Bra_Positions.Peek
            Dim Current_Ket_Position As Integer = Args_String.IndexOf("#"c, 0) + 1
            arrArgs = Args_String.Substring(Current_Ket_Position, Current_Bra_Position - Current_Ket_Position).Split("#"c)
            While Ket_Positions.Count > 0
                Current_Bra_Position = Bra_Positions.Pop
                Current_Ket_Position = Ket_Positions.Peek
                'Dim nestling_levels As Short = 0
                While Bra_Positions.Peek < Current_Ket_Position
                    Bra_Positions.Pop()
                    Ket_Positions.Pop()
                    'nestling_levels = nestling_levels + 1
                End While
                Current_Ket_Position = Ket_Positions.Pop()
                arrArgs(arrArgs.Length - 1) = arrArgs(arrArgs.Length - 1) & Args_String.Substring(Current_Bra_Position, Current_Ket_Position - Current_Bra_Position + 1)
                Dim temp_arrArgs() As String
                temp_arrArgs = Args_String.Substring(Args_String.IndexOf("#"c, Current_Ket_Position) + 1, Bra_Positions.Peek - Args_String.IndexOf("#"c, Current_Ket_Position) - 1).Split("#"c)
                ReDim Preserve arrArgs(arrArgs.Length + temp_arrArgs.Length - 1)
                temp_arrArgs.CopyTo(arrArgs, arrArgs.Length - temp_arrArgs.Length)
            End While
        End If

        ReDim Parameters(arrArgs.Length - 1)
        ReDim Parameter_Values(arrArgs.Length - 1)

        For i = 0 To arrArgs.Length - 1
            Parameters(i) = arrArgs(i).Substring(0, arrArgs(i).IndexOf(" ", 0))
            Parameter_Values(i) = arrArgs(i).Substring(arrArgs(i).IndexOf(" ", 0) + 1, arrArgs(i).Length - Parameters(i).Length - 1)
        Next
        Return 0
    End Function


    Sub Read_File(ByVal file_name As String, ByVal Initial_Frame As Integer, ByVal Final_Frame As Integer, _
                  ByVal Detector_X_Dimention As Integer, ByVal Detector_Y_Dimention As Integer, _
                   ByVal Frame_Data_Type_Number_Of_Bytes As Integer, _
                     ByVal low As Short, ByVal high As Short, ByVal Min_Intensity As Short, ByVal low1 As Short, ByVal high1 As Short)


        Dim str As FileStream
        Dim reader1 As BinaryReader
        str = New FileStream(file_name, FileMode.Open)

        reader1 = New BinaryReader(str)

        reader1.ReadBytes(2050 * 2)

        For i As Integer = 1 To Initial_Frame - 1
            reader1.ReadBytes(Detector_X_Dimention * Detector_Y_Dimention * Frame_Data_Type_Number_Of_Bytes)
        Next

        Dim Frame_Size_In_Bites = Detector_X_Dimention * Detector_Y_Dimention * Frame_Data_Type_Number_Of_Bytes
        'Dim Frame_To_Analyze_In_Bytes() As Byte
        Dim Frame_To_Analyze(,) As Integer
        'ReDim Frame_To_Analyze_In_Bytes(Frame_Size_In_Bites - 1)
        ReDim Frame_To_Analyze(Detector_X_Dimention - 1, Detector_Y_Dimention - 1)
        Dim xloop As Integer
        Dim yloop As Integer
        Dim readValue As Int16
        For i As Integer = Initial_Frame To Final_Frame
            'Frame_To_Analyze_In_Bytes = reader1.ReadBytes(Frame_Size_In_Bites)
            'CopyMemory(Frame_To_Analyze(0, 0), Frame_To_Analyze_In_Bytes(0), Frame_Size_In_Bites)    
            xloop = 0
            yloop = 0
            For kloop = 0 To Detector_X_Dimention * Detector_Y_Dimention - 1
                readValue = reader1.ReadInt16()

                Frame_To_Analyze(xloop, yloop) = readValue

                xloop = xloop + 1
                If xloop = 1340 Then
                    xloop = 0
                    yloop = yloop + 1
                End If
            Next

            'Static Dim Spectrum_ As New DocFile
            'Spectrum_.Open("", Detector_X_Dimention, Detector_Y_Dimention, 1, dataType.X_LONG, "")
            'Spectrum_.PutFrame(1, Frame_To_Analyze)
            Analyze(file_name, Frame_To_Analyze, Final_Frame - Initial_Frame + 1, _
                    low, high, Min_Intensity, low1, high1, _
                    Detector_X_Dimention, Detector_Y_Dimention)
        Next

    End Sub



End Module
